package com.bruce.learnrecycler.kotlin.ui.activity

import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.os.PersistableBundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.StaggeredGridLayoutManager
import android.view.LayoutInflater
import com.bruce.learnrecycler.kotlin.action.ActionDelegate
import com.bruce.learnrecycler.kotlin.store.MainStore
import com.bruce.learnrecycler.kotlin.ui.adapter.MainAdapter
import com.bruce.learnrecycler.kotlin.ui.listitem.Item
import com.bruce.learnrecycler.kotlin.ui.utils.ItemTouchBuilder
import com.bruce.learnrecylerview.app.R
import com.bruce.learnrecylerview.app.action.MainActions
import com.bruce.learnrecylerview.app.base.BaseApplication
import com.github.bluzwang.fluxsupportkotlin.BaseFluxActivity
import com.github.bluzwang.fluxsupportkotlin.FluxResponse
import com.github.bluzwang.fluxsupportkotlin.Store
import com.github.bluzwong.kotlin_x_weapon.EndlessAdapterWrapper
import com.github.bluzwong.kotlin_x_weapon.setOnItemClickListener
import com.github.bluzwong.kotlin_x_weapon.toast
import kotlinx.android.synthetic.activity_main.recyclerViewMain
import javax.inject.Inject

/**
 * Created by wangzhijie@wind-mobi.com on 2015/9/2.
 */
public class MainActivityKt : BaseFluxActivity() {

    var mainStore: MainStore? = null
        @Inject set
    var delegate: ActionDelegate? = null
        @Inject set
    var adapter: MainAdapter? = null
        @Inject set
    var inflater: LayoutInflater ? = null
        @Inject set

    val hashCode = hashCode()
    var adapterWrapper: EndlessAdapterWrapper? = null
    override fun provideContentId(): Int = R.layout.activity_main
    override fun provideStore(): Store? = mainStore
    override fun init() {
        BaseApplication.getDagger() inject this
        setRecyclerView()
    }

    override fun onRestoreView() {
        adapterWrapper?.loadFinish()
    }

    override fun onNewView() {
        for (i in 0..2) {
            delegate?.requestByLineNameStationName(hashCode, "89", "田林路桂林路", true, 0)
        }
    }

    private fun setRecyclerView() {
        adapterWrapper = EndlessAdapterWrapper(recyclerViewMain, adapter!!, inflater!!, R.layout.twoball_loading)
        adapterWrapper?.setOnLoadListener {
            delegate?.requestByLineNameStationName(hashCode, "89", "田林路桂林路", true)
        }

        val layoutManager = if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        } else LinearLayoutManager(this)

        recyclerViewMain setLayoutManager layoutManager
        recyclerViewMain setOnItemClickListener {
            //delegate?.requestByLineNameStationName(hashCode, "166", "桂林路田林路", true, it.getAdapterPosition())
            when (it.getAdapterPosition()) {
                adapter!!.items!!.count() -> "正在加载中 ".toast(this)
                else -> startActivity(Intent(this, javaClass<NextActivity>()))
            }
        }
        ItemTouchBuilder()
                .setSwipe { holder, size, flags ->
                    if (holder.getAdapterPosition() < adapter!!.items!!.count()) {
                        flags.canSwipeLeft()
                    } else flags
                }
                .setDrag { holder, size, flags ->
                    val adapterSize = adapter!!.items!!.count()
                    when (holder.getAdapterPosition()) {
                        adapterSize - 1 -> flags.canDragUp()
                        adapterSize -> flags
                        else -> flags.canDragUp().canDragDown()
                    }
                }
                .onSwipe { vh, size, direction ->
                    mainStore?.requestRemoveItem(vh.getAdapterPosition())
                }
                .onDrag { rv, size, vh1, vh2 ->
                    if (vh2.getAdapterPosition() != adapter!!.items!!.count()) {
                        mainStore?.requestSwapItem(vh1.getAdapterPosition(), vh2.getAdapterPosition())
                        true
                    } else false
                } attachToRecyclerView recyclerViewMain
    }

    //MainThread
    //Subscribe(threadMode = ThreadMode.MainThread)
    override public fun onResponse(type: String, dataMap: Map<String, Any>) {
        if (dataMap containsKey MainActions.KEY_DATAS) {
            adapter?.items = (dataMap get MainActions.KEY_DATAS) as MutableList<Item>
        }
        when (type) {
            MainActions.UI_ADD_ONE_ITEM -> {
                val insertIndex = (dataMap get MainActions.KEY_ITEM_INDEX) as Int
                //                    adapterWrapper?.notifyItemInserted(insertIndex)
                adapterWrapper?.notifyDataSetChanged()
                adapterWrapper?.loadFinish()
                if (adapter!!.items!!.size() > 10) {
                    // adapterWrapper?.hasNoMore()
                }

            }
            MainActions.UI_REMOVE_ONE_ITEM -> {
                adapterWrapper?.notifyItemRemoved((dataMap get MainActions.KEY_ITEM_INDEX) as Int)
                adapterWrapper?.reset()
            }
            MainActions.UI_SWAP_ONE_ITEM -> {
                val from = (dataMap get MainActions.KEY_ITEM_INDEX) as Int
                val to = (dataMap get MainActions.KEY_ITEM_INDEX2) as Int
                adapterWrapper?.notifyItemMoved(from, to)
            }
        }
    }
}